package com.checcdata.cordova.plugin;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

import com.checcdata.cordova.activity.AMapNavigationActivity;
import com.checcdata.cordova.activity.AMapTruckNavigationActivity;
import com.checcdata.cordova.service.AMapLocationService;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PermissionHelper;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 高德地图 api http://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
 */
public class AMapPlugin extends CordovaPlugin {

    protected final static String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
    public static final int ACCESS_LOCATION = 1;

    private Context context;
    private CallbackContext callbackContext = null;
    private AMapLocationService aMapLocationService = null;

    private static AMapPlugin aMapPlugin = null;

    private final String GET_CURRENT_LOCATION = "getCurrentLocation";

    private final String NAVIGATION = "navigation";

    private final String TRUCK_NAVIGATION = "truckNavigation";

    public static AMapPlugin getInstance() {
        return aMapPlugin;
    }

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        context = this.cordova.getActivity().getApplicationContext();
        aMapLocationService = new AMapLocationService(context);
        super.initialize(cordova, webView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;
        if (action.equals(GET_CURRENT_LOCATION)) {
            if (context.getApplicationInfo().targetSdkVersion < 23) {
                aMapLocationService.getLocation(callbackContext);
                return true;
            } else {
                boolean access_fine_location = PermissionHelper.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
                boolean access_coarse_location = PermissionHelper.hasPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
                if (access_fine_location && access_coarse_location) {
                    aMapLocationService.getLocation(callbackContext);
                    return true;
                } else {
                    PermissionHelper.requestPermissions(this, ACCESS_LOCATION, permissions);
                    return false;
                }
            }
        } else if (action.equals(NAVIGATION)) {
            Log.i("result", "Navigation");
            new AMapNavigationActivity(this.cordova.getActivity().getApplicationContext(), args);
            return true;
        } else if (action.equals(TRUCK_NAVIGATION)) {
            Log.i("result", "truckNavigation");
            new AMapTruckNavigationActivity(this.cordova.getActivity().getApplicationContext(), args);
            return true;
        }
        return false;
    }

    @Override
    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
        switch (requestCode) {
            case ACCESS_LOCATION:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    aMapLocationService.getLocation(callbackContext);
                } else {
                    Toast.makeText(this.cordova.getActivity(), "请开启应用定位权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    @SuppressLint("LongLogTag")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 100) {
            JSONObject json = new JSONObject();
            try {
                if (resultCode == Activity.RESULT_CANCELED) {
                    json.put("status", -1);
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json);
                    callbackContext.sendPluginResult(pluginResult);
                } else {
                    json.put("status", 0);
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json);
                    callbackContext.sendPluginResult(pluginResult);
                }
            } catch (JSONException ex) {
                Log.e("AMapNavigation.onActivityResult", ex.getMessage());
            }
        }
    }

}
